import pymysql.cursors
import numpy as np
import re
from collections import Counter
connection = pymysql.connect(host='localhost',
                             user='root',
                             password='123456',
                             database='testbase',
                             cursorclass=pymysql.cursors.DictCursor)
cursor=connection.cursor()

sql="select * from salary where type=2"

cursor.execute(sql)
result=cursor.fetchall()
def getScore():
    results=[]
    for item in result:
        results.append(item["skills"].replace(" ","").split("\n"))

    temp=[]
    for item in results:
        temp+=item
    temps=[]

    for item in temp:
        if item:
            temps.append(item)

    counInfo=Counter(temps)
    print(counInfo)
    counInfo=counInfo.most_common()

    counInfoList=[]

    for item in counInfo:
        counInfoList.append(list(item))

    sums=0

    for item in counInfoList:
        sums+=item[1]
    for item in counInfoList:
        item[1]=item[1]/sums

    counInfoDict={}
    for item in counInfoList:
        counInfoDict[item[0]]=item[1]

    return counInfoDict

# 以字典的方式来记录技能对应的权重(分值)
score=getScore()


X=[]
Y=[]
cityMark={"太原":1,"北京":2}

eduMark={"大专":1,"本科":2,"硕士":3,"博士":4,"不限专业":0}

for item in result:
    str1 = item["salary"]
    if not (re.findall("天", str1)) and re.findall("(千|万)", str1):
        a=[]
        a.append(1)
        # 城市
        a.append(cityMark[item["city"]])
        #学历
        edu=item["education"].replace(" ","").replace("\n","")
        a.append(eduMark.get(edu,0))
        #工作年限
        workage=item["workage"].replace(" ","").replace("\n","")
        if re.findall("\d",workage):
            workage=workage[:1]
        else:
            workage=1
        a.append(workage)
        #技能值
        scores=0
        for i in item["skills"].replace(" ","").split("\n"):
            if i:
                scores+=score[i]
        print(scores)
        a.append(scores)
        X.append(a)

        nums=re.findall("\d+\.{0,1}\d*",str1)
        flag=re.findall("(千|万)",str1)
        num1=float(nums[0])*1000 if flag[0]=="千" else float(nums[0])*10000
        num2=float(nums[1])*1000 if flag[1]=="千" else float(nums[1])*10000
        avg=(num1+num2)/2
        Y.append(avg)

X=np.array(X).astype(float)
Y=np.array(Y)
print(X)
print(Y)
theta=np.linalg.pinv(X.T.dot(X)).dot(X.T).dot(Y)
print(theta)
f=open("theta.py","w")
f.write("import numpy as np \ntheta="+str(list(theta)))


